package me.huqiao.loganlyzer.util;

import java.util.ArrayList;
import java.util.List;

public class StringUtil {

	public static boolean isEmpty(String str){
		return str == null || str.trim().equals("") || "null".equals(str.toLowerCase());
	}

	public static String getStrFilterNull(String str,String defaultValue){
		return isEmpty(str)?defaultValue:str;
	}

	public static String getStrFilterNull(Double str,String defaultValue){
		return getStrFilterNull(String.valueOf(str),defaultValue);
	}

	public static String getStrFilterNull(int str,String defaultValue){
		return getStrFilterNull(String.valueOf(str),defaultValue);
	}

	/**
	 * 拆分字符串，不包含空格 []作为一个整体
	 * @param input
	 * @return
	 */
	public static List<String> split(String input) {
		List<String> result = new ArrayList<>();
		StringBuilder token = new StringBuilder();
		int bracketLevel = 0;

		for (char c : input.toCharArray()) {
			if (c == '[') {
				bracketLevel++;
			} else if (c == ']') {
				bracketLevel--;
			}
			if (bracketLevel == 0 && c == ' ') {
				if (token.length() > 0) {
					result.add(token.toString());
					token = new StringBuilder();
				}
			} else {
				token.append(c);
			}
		}
		if (token.length() > 0) {
			result.add(token.toString());
		}

		return result;
	}
}
